using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CMS_SUN_IF_Extraction
{
    public partial class frmAppend_AB_Detail_SUN_Form : Form
    {
        private BAL bao = new BAL();
        private DataTable tbMain = null;
        DateTime uCtrl_Start_Date, uCtrl_End_date;
        string uVAT1_center, uVAT2_center, uVAT3_center, uVAT4_center, uVAT5_center;
        string uVAT1_StartNum, uVAT2_StartNum, uVAT3_StartNum, uVAT4_StartNum, uVAT5_StartNum;
        string uVAT1_EndNum, uVAT2_EndNum, uVAT3_EndNum, uVAT4_EndNum, uVAT5_EndNum;
        string uCur_Start_ExtrDate, uCur_End_ExtrDate, uCur_VAT_Center, uCur_Start_VAT_num, uCur_End_VAT_num;
        private bool flag = false;
        private int uAcct_period = 0;

        public frmAppend_AB_Detail_SUN_Form()
        {
            InitializeComponent();
        }

        private void frmAppend_AB_Detail_SUN_Form_Load(object sender, EventArgs e)
        {
            string t = "Appending Records into Extraction Detail table \n for Verification Check";
            label1.TextAlign = ContentAlignment.MiddleCenter;
            label1.Text = t;

            Get_SysControl();

            DataTable tblVATcenter = bao.get_lst_VAT_center();
            if (tblVATcenter != null)
            {
                cbVATCenter.DataSource = tblVATcenter;
                cbVATCenter.DisplayMember = "VAT Center";
                cbVATCenter.ValueMember = "VAT Center";
            }
            flag = true;
        }

        private void Get_SysControl()
        {
            tbMain = bao.get_tbl_SysControl();
            if (tbMain == null || tbMain.Rows.Count <= 0 )
                return;
            DataRow r = tbMain.Rows[0];            
            txtCtrlStartDate.Text = DateTime.Parse(r["Ctrl_Start_Date"].ToString()).ToShortDateString();
            txtCtrlEndDate.Text = DateTime.Parse(r["Ctrl_End_Date"].ToString()).ToShortDateString();

            uCtrl_Start_Date = DateTime.Parse(r["Ctrl_Start_Date"].ToString());
            uCtrl_End_date = DateTime.Parse(r["Ctrl_End_Date"].ToString());
            uVAT1_center = r["Ctrl_VAT1_Center"] != null ? r["Ctrl_VAT1_Center"].ToString() : "";            
            uVAT1_StartNum = r["Ctrl_VAT1_StartNum"] != null? r["Ctrl_VAT1_StartNum"].ToString() : "";
            uVAT1_EndNum = r["Ctrl_VAT1_EndNum"]!=null? r["Ctrl_VAT1_EndNum"].ToString():"";
            
            uVAT2_center = r["Ctrl_VAT2_Center"]!=null? r["Ctrl_VAT2_Center"].ToString():"";
            uVAT2_StartNum = r["Ctrl_VAT2_StartNum"]!=null?r["Ctrl_VAT2_StartNum"].ToString():"";
            uVAT2_EndNum = r["Ctrl_VAT2_EndNum"]!=null? r["Ctrl_VAT2_EndNum"].ToString():"";
                
            uVAT3_center = r["Ctrl_VAT3_Center"]!=null? r["Ctrl_VAT3_Center"].ToString():"";
            uVAT3_StartNum = r["Ctrl_VAT3_StartNum"]!=null? r["Ctrl_VAT3_StartNum"].ToString():"";
            uVAT3_EndNum = r["Ctrl_VAT3_EndNum"]!=null?r["Ctrl_VAT3_EndNum"].ToString():"";
                
            uVAT4_center = r["Ctrl_VAT4_Center"]!=null ? r["Ctrl_VAT4_Center"].ToString():"";
            uVAT4_StartNum = r["Ctrl_VAT4_StartNum"] != null? r["Ctrl_VAT4_StartNum"].ToString():"";
            uVAT4_EndNum = r["Ctrl_VAT4_EndNum"] !=null ? r["Ctrl_VAT4_EndNum"].ToString():"";
           
            uVAT5_center = r["Ctrl_VAT5_Center"] !=null? r["Ctrl_VAT5_Center"].ToString():"";
            uVAT5_StartNum = r["Ctrl_VAT5_StartNum"] != null ? r["Ctrl_VAT5_StartNum"].ToString() : "";
            uVAT5_EndNum = r["Ctrl_VAT5_EndNum"] !=null?r["Ctrl_VAT5_EndNum"].ToString():"";

            uAcct_period = r["Ctrl_Account_period"] != null ? int.Parse(r["Ctrl_Account_period"].ToString()) : 0;
            uCur_Start_ExtrDate = r["Curr_Start_ExtractDate"] != null ? r["Curr_Start_ExtractDate"].ToString() : "";
            uCur_End_ExtrDate = r["Curr_End_ExtractDate"] != null ? r["Curr_End_ExtractDate"].ToString() : "";
            uCur_VAT_Center = r["Curr_VAT_Center"] != null ? r["Curr_VAT_Center"].ToString() : "";
            uCur_Start_VAT_num = r["Curr_Start_VAT_num"] != null ? r["Curr_Start_VAT_num"].ToString() : "";
            uCur_End_VAT_num = r["Curr_End_VAT_num"] != null ? r["Curr_End_VAT_num"].ToString() : "";

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExtract_Click(object sender, EventArgs e)
        {
            if (cbVATCenter.SelectedValue == null)
            {
                MessageBox.Show("Please select a VAT Center !!");
                cbVATCenter.Focus();
                return;
            }

            if (dtpStartDate.Value > dtpEndDate.Value)
            {
                MessageBox.Show("START Date must not > than END Date !!");
                dtpStartDate.Focus();
                return;
            }

            if (txtVATStartNum.Text.Trim() == "")
            {
                MessageBox.Show("You must specify a START VAT Number !!");
                txtVATStartNum.Focus();
                return;
            }
            if (txtVATEndNum.Text.Trim() == "")
            {
                MessageBox.Show("You must specify an END VAT Number !!");
                txtVATEndNum.Focus();
                return;
            }
            if (bao.isNumeric(txtVATStartNum.Text) == false)
            {
                MessageBox.Show("Please input VAT start number!");
                txtVATStartNum.Focus();
                return;
            }
            if (bao.isNumeric(txtVATEndNum.Text) == false)
            {
                MessageBox.Show("Please input VAT start number!");
                txtVATEndNum.Focus();
                return;
            }

            if (int.Parse(txtVATStartNum.Text.Trim()) > int.Parse(txtVATEndNum.Text.Trim()))
            {
                MessageBox.Show("END VAT Number must be > than START VAT Number !! ");
                txtVATEndNum.Focus();
                return;
            }
            //==============================
            if (dtpStartDate.Value < DateTime.Parse( txtCtrlStartDate.Text.Trim()))
            {
                MessageBox.Show("START Date must not < than Control START Date !!");
                dtpStartDate.Focus();
                return;
            }
            if (dtpEndDate.Value > DateTime.Parse(txtCtrlEndDate.Text.Trim()))
            {
                MessageBox.Show("END Date must not > than Control END Date !!");
                dtpEndDate.Focus();
                return;
            }

            if (int.Parse(txtVATStartNum.Text) <  int.Parse(txtCtrlStartVATNum.Text))
            {
                MessageBox.Show("START VAT Number can not < Control Start VAT Number !!");
                txtVATStartNum.Focus();
                return;
            }

            if (int.Parse(txtVATEndNum.Text) > int.Parse(txtCtrlEndVATNum.Text))
            {
                MessageBox.Show("End VAT Number can not > Control End VAT Number !!");
                txtVATEndNum.Focus();
                return;
            }

            //==================Processing=============================
            int t = 0;
            lblStatus.Text = "Step 1 - Delete AB_Detail_SUN table ";
            bao.Delete_AB_Detail_SUN();

            lblStatus.Text = "Step 2 - Updating Drugs & Disposable's T2 Code";
            //t = bao.Upd_Drug_T2Code();
            //modified  on 07-Sep-2009
            t = bao.Upd_Drug_T2Code(dtpStartDate.Value.ToShortDateString(),
                                dtpEndDate.Value.ToShortDateString());
            //end

            lblStatus.Text = "Step 3 - AppEnding Medical Procedure codes into AB_Detail_SUN table";
            t = bao.SUN_SumSTDCode(cbVATCenter.SelectedValue.ToString(),
                                dtpStartDate.Value.ToShortDateString(),
                                dtpEndDate.Value.ToShortDateString(),                               
                                uAcct_period);
            lblStatus.Text = "Step 4 - AppEnding Service Tax entries into AB_Detail_SUN table";
            t = bao.SUN_SERV(cbVATCenter.SelectedValue.ToString(),
                            dtpStartDate.Value.ToShortDateString(),
                            dtpEndDate.Value.ToShortDateString(),
                            uAcct_period);
            lblStatus.Text = "Step 5 - AppEnding VAT Tax entries into AB_Detail_SUN table";
            t = bao.SUN_VAT(cbVATCenter.SelectedValue.ToString(),
                            dtpStartDate.Value.ToShortDateString(),
                            dtpEndDate.Value.ToShortDateString(),
                            uAcct_period);
            lblStatus.Text = "Step 6 - AppEnding TOTAL entries into AB_Detail_SUN table";
            t = bao.SUN_TOTAL(cbVATCenter.SelectedValue.ToString(),
                            dtpStartDate.Value.ToShortDateString(),
                            dtpEndDate.Value.ToShortDateString(),
                            uAcct_period);
            lblStatus.Text = "Step 7 - AppEnding records for checking of Missing VAT ..";
            t = bao.Append_VAT_Check_Table(cbVATCenter.SelectedValue.ToString(),
                                        int.Parse(txtVATStartNum.Text.Trim()),
                                        int.Parse(txtVATEndNum.Text.Trim()));

            //' -------- updating to the Curr fields
            
            GlobalClass.gCurr_VAT_Center = cbVATCenter.SelectedValue.ToString();
            GlobalClass.gCurr_Start_ExtractDate = dtpStartDate.Value.ToShortDateString();
            GlobalClass.gCurr_End_ExtractDate = dtpEndDate.Value.ToShortDateString();
            GlobalClass.gCurr_Start_VAT_Num = txtVATStartNum.Text.Trim();
            GlobalClass.gCurr_End_VAT_num = txtVATEndNum.Text.Trim();
            GlobalClass.gLast_Extract_Date = tbMain.Rows[0]["Last_Extract_Date"].ToString();
                       
            bao.update_tSysControl( cbVATCenter.SelectedValue.ToString(),
                                    txtCtrlStartDate.Text,
                                    txtCtrlEndDate.Text,
                                    dtpStartDate.Value.ToShortDateString(),
                                    dtpEndDate.Value.ToShortDateString(),
                                    txtVATStartNum.Text.Trim(),
                                    txtVATEndNum.Text.Trim());
            lblStatus.Text = "Process Completed !!!!!!!! ";
            MessageBox.Show("Process completed!");
        }

        private void cbVATCenter_SelectedValueChanged(object sender, EventArgs e)
        {
            if (flag == false)
                return;
            string uCurr_VATCenter;
            string vatNum = "";

            if (cbVATCenter.SelectedValue != null)
            {
                uCurr_VATCenter = cbVATCenter.SelectedValue.ToString();
                // -- to get the Start & End VAT Ctrl number ---

                if (uCurr_VATCenter.Equals(uVAT1_center))
                {
                    vatNum = "00000" + uVAT1_StartNum;
                    txtCtrlStartVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                    vatNum = "00000" + uVAT1_EndNum;
                    txtCtrlEndVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                }
                else if (uCurr_VATCenter.Equals(uVAT2_center))
                {
                    vatNum = "00000" + uVAT2_StartNum;
                    txtCtrlStartVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                    vatNum = "00000" + uVAT2_EndNum;
                    txtCtrlEndVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                }
                else if (uCurr_VATCenter.Equals(uVAT3_center))
                {
                    vatNum = "00000" + uVAT3_StartNum;
                    txtCtrlStartVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                    vatNum = "00000" + uVAT3_EndNum;
                    txtCtrlEndVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                }
                else if (uCurr_VATCenter.Equals(uVAT4_center))
                {
                    vatNum = "00000" + uVAT4_StartNum;
                    txtCtrlStartVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                    vatNum = "00000" + uVAT4_EndNum;
                    txtCtrlEndVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                }
                else if (uCurr_VATCenter.Equals(uVAT5_center))
                {
                    vatNum = "00000" + uVAT5_StartNum;
                    txtCtrlStartVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                    vatNum = "00000" + uVAT5_EndNum;
                    txtCtrlEndVATNum.Text = vatNum.Substring(vatNum.Length - 5);
                }
                else
                {
                    txtCtrlStartVATNum.Text = "";
                    txtCtrlEndVATNum.Text = "";
                    MessageBox.Show("the VAT Center you select does not have exist in the System Control table !!");
                    cbVATCenter.Focus();
                }
            }
        }

        private void txtVATStartNum_Leave(object sender, EventArgs e)
        {
            string t = "00000" + txtVATStartNum.Text.Trim();
            txtVATStartNum.Text = t.Substring(t.Length - 5);
        }

        private void txtVATStartNum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtVATStartNum_Leave(sender, new EventArgs());
            }
        }

        private void txtVATEndNum_Leave(object sender, EventArgs e)
        {
            string t = "00000" + txtVATEndNum.Text.Trim();
            txtVATEndNum.Text = t.Substring(t.Length - 5);
        }

        private void txtVATEndNum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtVATEndNum_Leave(sender, new EventArgs());
        }     

        private void cbVATCenter_Leave(object sender, EventArgs e)
        {
            if (cbVATCenter.SelectedValue.ToString() == "*ALL")
                cbVATCenter_SelectedValueChanged(sender, new EventArgs());
        }
    }
}